<?php
class Product_list extends MY_Front_Controller {

	function __construct() {
		parent::__construct();
		$this->load->helper('url');
	}
	
	public function index(){
	}
	
	public function get_list($count = 0,$cat_id = 0,$filter_string = ''){
	
		$filter = array();
		
		$filter = explode('-',$filter_string);
		
		$this->load->model('product_model');
		$this->load->library('pagination');
		$this->load->library('user_agent');
		
		$session_search = $this->session->userdata('product_filter');
		$search = (isset($session_search) ? $session_search : array());
		
		$search['current_url'] = current_url();

		if (isset($filter['search-price']) AND strlen($filter['search-price'])){
			$price_arr = explode(',',$filter['search-price']);
			
			$search['price_more'] = $price_arr[0];
			if (isset($price_arr[1])){
				$search['price_less'] = $price_arr[1];
			}
		} else {
			if (isset($search['price_more'])){
				unset($search['price_more']);
			}
		if (isset($search['price_less'])){
				unset($search['price_less']);
			}
		}
		
		if ($cat_id){
			$search['category'] = $cat_id;
		} else {
			if (isset($search['category'])){
				unset($search['category']);
			}
		}
		
		if (isset($filter['search-metal']) AND strlen($filter['search-metal'])){
			$search['metal'] = $filter['search-metal'];
		} else {
			if (isset($search['metal'])){
				unset($search['metal']);
			}
		}
	
		if (isset($filter['search-gem']) AND strlen($filter['search-gem'])){
			$search['gem'] = $filter['search-gem'];
		} else {
			if (isset($search['gem'])){
				unset($search['gem']);
			}
		}
		
		$this->session->set_userdata(array('product_filter' => $search));
		
		$search['language_id'] = $this->config->item('lang_obj')->id;
				
		$search['per_page'] = (isset($filter['limit']) ? $filter['limit'] : $this->config->item('products_per_page'));
	
		$config['base_url'] = site_url();
		$config['total_rows'] = $this->product_model->getProductList($search,true)->result();
		$config['total_rows'] = $config['total_rows'][0]->count;
		$config['per_page'] = $search['per_page'];
		$config['uri_segment'] = 2;
		$config['num_links'] = 3;
		$config['first_link'] = lang('first_elem');
		$config['last_link'] = lang('last_elem');
		
		$search['to'] = $config['per_page'];
		$search['from'] = $count;		
		
		$data['products'] = $this->product_model->getProductList($search)->result();
		
		$data['categories'] = $this->category_model->getAll(array('parent_id' => 0,'language_id' => $this->config->item('lang_obj')->id))->result();
		$data['metals'] = explode(',',$this->config->item('metals_for_filter'));
	
		$this->pagination->initialize($config);
		
		$data['links'] = $this->pagination->create_links();
	
		$data['search'] = $search;

		$this->template->add_js('application/assets/js/product_list.js');
		$this->template->write_view ( 'content', 'front/list_view.php', $data );
	  	$this->template->render ();
	}
}